﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using System.Threading;
using System.IO;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;
using System.IO.Ports;
using weiqijiance;
using ZedGraph;
using CCWin;
using Newtonsoft.Json;
namespace weiqijiance
{
    public partial class btgyiqijianding : CCSkinMain
    {
        public mainform m_mainform;
        public bool bldpy;
        public bool bSK;
        public bool bnox;
        public bool ldpy;
        public bool bssxysj;
        public bool bxxxysj;
        public DateTime tqsj;
        public DateTime tztqsj;
        string sxianshineirong = "";
        Thread t2;
        public btgyiqijianding()
        {
            InitializeComponent();
            timer1.Enabled = false;
            textBox3.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            txtstartxl.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            bnox = false;
            timer1.Enabled = true;
        }

        private void skinButton3_Click(object sender, EventArgs e)
        {
            txtendxl.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            ///记录检定数据      
            ///
            iniclass objini = new iniclass();
            objini.Ini(System.Windows.Forms.Application.StartupPath + "\\设备检定结果.ini");

            objini.Writue("不透光检定", "标准NS0%", k0bzz.Text);
            objini.Writue("不透光检定", "检定NS0%", k0cyz.Text);
            objini.Writue("不透光检定", "标准NS30%", k30bzz.Text);
            objini.Writue("不透光检定", "检定NS30%", k30cyz.Text);
            objini.Writue("不透光检定", "标准NS50%", k50bzz.Text);
            objini.Writue("不透光检定", "检定NS50%", k50cyz.Text);
            objini.Writue("不透光检定", "标准NS70%", k70bzz.Text);
            objini.Writue("不透光检定", "检定NS70%", k70cyz.Text);
            objini.Writue("不透光检定", "标准NS100%", k100bzz.Text);
            objini.Writue("不透光检定", "检定NS100%", k100cyz.Text);
            objini.Writue("不透光检定", "判定", text_dpjg.Text);
            objini.Writue("不透光检定", "检定时间", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));


            string sjieguo = string.Format(@"不透光N值标准值{0};不透光N值检定值{1}",
                                                    k0cyz.Text,
                                                    k0bzz.Text);
            string ssqlex = string.Format(@"insert into JDSJ (
                                               JDSJ_MC,
                                               JDSJ_JG,
                                               JDSJ_RQ,
                                               JDSJ_JCX) values (
                                                '{0}',
                                                '{1}',
                                                '{2}',
                                                '{3}')",
                                         "不透光烟度计",
                                         sjieguo,
                                         DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                         m_mainform.lineCode);
            m_mainform.objbassLW.objdb.InsertDB(ssqlex);

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            m_mainform.objbtg.SendDeviceCmd(3);

            Thread.Sleep(500);
            if (bnox)
            {
                no_cyz.Text = string.Format(@"{0}", m_mainform.objbtg.m_fnosk.ToString("0"));
                co2cyz.Text = string.Format(@"{0}", m_mainform.objbtg.m_fco2sk.ToString("0.00"));
                shuaxin();//计算绝对值、相对值
            }
            if (!bnox)
            {
                text_kzhi.Text = string.Format(@"{0}", m_mainform.objbtg.m_fnssk.ToString("0.00"));
                //if (bns0)
                //    k0cyz.Text = string.Format(@"{0}", m_mainform.objbtg.m_fnssk.ToString("0.00"));
                //if (bns30)
                //    k30cyz.Text = string.Format(@"{0}", m_mainform.objbtg.m_fnssk.ToString("0.00"));
                //if (bns50)
                //    k50cyz.Text = string.Format(@"{0}", m_mainform.objbtg.m_fnssk.ToString("0.00"));
                //if (bns70)
                //    k70cyz.Text = string.Format(@"{0}", m_mainform.objbtg.m_fnssk.ToString("0.00"));
                //if (bns100)
                //    k100cyz.Text = string.Format(@"{0}", m_mainform.objbtg.m_fnssk.ToString("0.00"));
                //shuaxin();//计算绝对值、相对值            
                
            }


           

            if (bldpy)
                getldpy();
            if (bt90start)
                gett90();
            if (bt10start)
                gett10();
        }

        private void skinButton4_Click(object sender, EventArgs e)
        {

            txtstartxl.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            {
                m_mainform.objbtg.SendDeviceCmd(15);
                Thread.Sleep(1000);
                t2 = new Thread(thread2cy);
                t2.Start();
            }
        }
        public string endtimexl = "";

        private void thread2cy()
        {
            DateTime date = DateTime.Now;

            sxianshineirong = "泄露检测中";
            xianshixinxi();
            Thread.Sleep(20000);
            //m_mainform.m_fxlbs = true;
            m_mainform.objbtg.m_fxlbs = true;
            while (true)
            {

                //m_mainform.SendDeviceCmdGas(0x61, 0x9A);//获取状态
                m_mainform.objbtg.SendDeviceCmd(6);
                Thread.Sleep(1000);
                if (!m_mainform.objbtg.m_fxlbs)
                {
                    sxianshineirong = "泄露检测完成";
                    break;
                }
                else
                {
                    sxianshineirong = "泄露检测中";
                }
                xianshixinxi();
                if ((DateTime.Now - date).TotalMilliseconds > 1000 * 40)
                {
                    break;
                }
            }
            int ixl = m_mainform.objbtg.m_fxl;
            if (ixl == 1)
            {
                sxianshineirong = "泄露不合格";
            }
            else
            {
                sxianshineirong = "泄露合格";
            }
            xianshixinxi();
            endtimexl = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            xianshixielouend();
            ///记录检定数据      
            string sjieguo = string.Format(@"泄露检测：{0}", sxianshineirong);
            string ssqlex = string.Format(@"insert into JDSJ (
                                               JDSJ_MC,
                                               JDSJ_JG,
                                               JDSJ_RQ,
                                               JDSJ_JCX) values (
                                                '{0}',
                                                '{1}',
                                                '{2}',
                                                '{3}')",
                                         "不透光",
                                         sjieguo,
                                         DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                         m_mainform.lineCode);
            m_mainform.objbassLW.objdb.InsertDB(ssqlex);
        }

        private void skinButton5_Click(object sender, EventArgs e)
        {
            model.尾气自检.xljc objxljc = new model.尾气自检.xljc();
            objxljc.dtCheckDate = txtstartxl.Text;
            objxljc.dtBeginTime = txtstartxl.Text;
            objxljc.dtEndTime = txtendxl.Text;
            objxljc.nJudegeResult = xl_fq.Text.Contains("合格") ? "1" : "2";
            objxljc.szRemark = "";
            objxljc.syq = "2";
            objxljc.jcry = skinCom_czy.Text; //m_mainform.InspectionOperator.Length<1 ? "蒋佩": m_mainform.InspectionOperator;
            MessageBox.Show(m_mainform.objbass.uploadzj(objxljc));
        }

        private delegate void FlushClient();//代理
        private void xianshixinxi()
        {
            if (this.xl_fq.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshixinxi);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.xl_fq.Text = string.Format(@"{0}", sxianshineirong);
            }
        }

        private void xianshixielouend()
        {
            if (this.txtendxl.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshixielouend);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.txtendxl.Text = string.Format(@"{0}", endtimexl);
            }
        }

        private void btgyiqijianding_Load(object sender, EventArgs e)
        {
            iniCZY("CZY");
            txtstartxl.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            txtendxl.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            textBox1.Text = m_mainform.stationCode;
            textBox2.Text = m_mainform.lineCode;
            textBox3.Text = DateTime.Now.ToString("yyyy-MM-dd");

            getqiti("柴油", CBBND.Text);
            getN();
            x = this.Width;
            y = this.Height;
            setTag(this);
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        }
        public void iniCZY(string dataname)
        {

            string ssqlex = "";
            //ssqlex = string.Format(@"select * from {0}", dataname);
            ssqlex = string.Format(@"select * from Employee where EP_TYPE ='引车员'");
            DataTable dt = m_mainform.objbass.objdb.GetTable(ssqlex);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                skinCom_czy.Items.Add(dt.Rows[i]["EP_NAME"].ToString().Trim());
                if (i == 0)
                    skinCom_czy.Text = dt.Rows[0]["EP_NAME"].ToString().Trim();
            }
        }
        #region 控件大小随窗体大小等比例缩放
        private float x;//定义当前窗体的宽度
        private float y;//定义当前窗体的高度
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
            {
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null)
                {
                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度
                    con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度
                    con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距
                    con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距
                    Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
        }
        #endregion
        private void textBox13_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void skinButton7_Click(object sender, EventArgs e)
        {
            model.尾气自检.btgydjjc objzjsj = new model.尾气自检.btgydjjc();
            objzjsj.CheckType0 = "0";
            objzjsj.CheckType30 = "30";
            objzjsj.CheckType50 = "50";
            objzjsj.CheckType70 = "70";
            objzjsj.CheckType100 = "100";
            objzjsj.CheckVal0 = k0cyz.Text;
            objzjsj.CheckVal30 = k30cyz.Text;
            objzjsj.CheckVal50 = k50cyz.Text;
            objzjsj.CheckVal70 = k70cyz.Text;
            objzjsj.CheckVal100 = k100cyz.Text;
            objzjsj.StdVal0 = k0bzz.Text;
            objzjsj.StdVal30 = k30bzz.Text;
            objzjsj.StdVal50 = k50bzz.Text;
            objzjsj.StdVal70 = k70bzz.Text;
            objzjsj.StdVal100 = k100bzz.Text;
            objzjsj.AbssoluteVal0 = k0jdc.Text;
            objzjsj.AbssoluteVal30 = k30jdc.Text;
            objzjsj.AbssoluteVal50 = k50jdc.Text;
            objzjsj.AbssoluteVal70 = k70jdc.Text;
            objzjsj.AbssoluteVal100 = k100jdc.Text;
            objzjsj.AbssoluteJudge0 = "1";
            objzjsj.AbssoluteJudge30 = "1";
            objzjsj.AbssoluteJudge50 = "1";
            objzjsj.AbssoluteJudge70 = "1";
            objzjsj.AbssoluteJudge100 = "1";
            objzjsj.Checker = skinCom_czy.Text;
            {
                iniclass objini = new iniclass();
                string path = "";
                path = System.Windows.Forms.Application.StartupPath + "\\检测自检\\自由加速设备自检信息.ini";
                string path1 = string.Format(@"{0}", path);
                objini.Ini(path1);

                if (k0cyz.Text.Length > 0)
                {
                    objini.Writue("记录", "N0", k0cyz.Text);
                    objini.Writue("记录", "N0_Error", k0jdc.Text);
                    objini.Writue("记录", "N0_Judge", "1");
                }
                if (k30cyz.Text.Length > 0)
                {
                    objini.Writue("记录", "N30", k30cyz.Text);
                    objini.Writue("记录", "N30_Error", k30jdc.Text);
                    objini.Writue("记录", "N30_Judge", "1");

                }
                if (k50cyz.Text.Length > 0)
                {
                    objini.Writue("记录", "N50", k50cyz.Text);
                    objini.Writue("记录", "N50_Error", k50jdc.Text);
                    objini.Writue("记录", "N50_Judge", "1");
                }
                if (k70cyz.Text.Length > 0)
                {
                    objini.Writue("记录", "N70", k70cyz.Text);
                    objini.Writue("记录", "N70_Error", k70jdc.Text);
                    objini.Writue("记录", "N70_Judge", "1");
                    //objini.Writue("记录", "N90", k90cyz.Text);
                    //objini.Writue("记录", "N90_Error", k90jdc.Text);
                    //objini.Writue("记录", "N90_Judge", "1");
                }
                if (k100cyz.Text.Length > 0)
                {
                    objini.Writue("记录", "N100", k100cyz.Text);
                    objini.Writue("记录", "N100_Error", k100jdc.Text);
                    objini.Writue("记录", "N100_Judge", "1");
                }
            }
            {
                iniclass objini = new iniclass();
                string path = "";
                path = System.Windows.Forms.Application.StartupPath + "\\检测自检\\加载减速设备自检信息.ini";
                string path1 = string.Format(@"{0}", path);
                objini.Ini(path1);
                if (k0cyz.Text.Length > 0)
                {
                    objini.Writue("记录", "N0", k0cyz.Text);
                    objini.Writue("记录", "N0_Error", k0jdc.Text);
                    objini.Writue("记录", "N0_Judge", "1");
                }
                if (k30cyz.Text.Length > 0)
                {
                    objini.Writue("记录", "N30", k30cyz.Text);
                    objini.Writue("记录", "N30_Error", k30jdc.Text);
                    objini.Writue("记录", "N30_Judge", "1");

                }
                if (k50cyz.Text.Length > 0)
                {
                    objini.Writue("记录", "N50", k50cyz.Text);
                    objini.Writue("记录", "N50_Error", k50jdc.Text);
                    objini.Writue("记录", "N50_Judge", "1");
                }
                if (k70cyz.Text.Length > 0)
                {
                    objini.Writue("记录", "N70", k70cyz.Text);
                    objini.Writue("记录", "N70_Error", k70jdc.Text);
                    objini.Writue("记录", "N70_Judge", "1");
                    //objini.Writue("记录", "N90", k90cyz.Text);
                    //objini.Writue("记录", "N90_Error", k90jdc.Text);
                    //objini.Writue("记录", "N90_Judge", "1");
                }
                if (k100cyz.Text.Length > 0)
                {
                    objini.Writue("记录", "N100", k100cyz.Text);
                    objini.Writue("记录", "N100_Error", k100jdc.Text);
                    objini.Writue("记录", "N100_Judge", "1");
                }
            }
            MessageBox.Show(m_mainform.objbass.uploadzj(objzjsj));
        }

        private void skinButton8_Click(object sender, EventArgs e)
        {

            model.尾气自检.zjgcsj objgcsjbzq = new model.尾气自检.zjgcsj();
            objgcsjbzq.dtCheckDate = txtstartxl.Text;
            objgcsjbzq.dtBeginTime = txtstartxl.Text;
            objgcsjbzq.dtEndTime = txtendxl.Text;
            string szCheckType = "3";
            if (CBBND.Text.Contains("中低浓度"))
            {
                szCheckType = "3";
            }
            else if (CBBND.Text.Contains("低浓度"))
            {
                szCheckType = "3";
            }
            else if (CBBND.Text.Contains("中高浓度"))
            {
                szCheckType = "5";
            }
            else if (CBBND.Text.Contains("高浓度"))
            {
                szCheckType = "5";
            }
            else if (CBBND.Text.Contains("零"))
            {
                szCheckType = "4";
            }
            objgcsjbzq.szCheckType = szCheckType;//1-加载滑行 2-附加损失 3-单点检查（低标 气）4-单点检查（零气）5-单点检查（高标气） 6-五点检查
            if (szCheckType != "5")
            {
                objgcsjbzq.icount = 3;
                int inogc = 0;
                int.TryParse(no_cyz.Text, out inogc);
                float ico2gc = 0;
                float.TryParse(co2cyz.Text, out ico2gc);

                objgcsjbzq.stringNO.Add(0);
                objgcsjbzq.stringCO2.Add(0);

                objgcsjbzq.stringNO.Add(inogc * 0.4f);
                objgcsjbzq.stringCO2.Add(ico2gc * 0.4f);

                objgcsjbzq.stringNO.Add(inogc);
                objgcsjbzq.stringCO2.Add(ico2gc);
            }
            else
            {
                objgcsjbzq.icount = 5;
                int inogc = 0;
                int.TryParse(no_cyz.Text, out inogc);
                float ico2gc = 0;
                float.TryParse(co2cyz.Text, out ico2gc);

                objgcsjbzq.stringNO.Add(0);
                objgcsjbzq.stringCO2.Add(0);

                objgcsjbzq.stringNO.Add(inogc * 0.4f);
                objgcsjbzq.stringCO2.Add(ico2gc * 0.4f);

                objgcsjbzq.stringNO.Add(inogc * 0.7f);
                objgcsjbzq.stringCO2.Add(ico2gc * 0.7f);

                objgcsjbzq.stringNO.Add(inogc * 0.9f);
                objgcsjbzq.stringCO2.Add(ico2gc * 0.9f);

                objgcsjbzq.stringNO.Add(inogc);
                objgcsjbzq.stringCO2.Add(ico2gc);
            }
            string smexshow = m_mainform.objbass.upzjgcsj(objgcsjbzq);
            if (smexshow.Length>1)
            {
                MessageBox.Show(smexshow);
            }
            

            model.尾气自检.btgnoxyqjd objzjsj = new model.尾气自检.btgnoxyqjd();
            objzjsj.CheckValNO = no_cyz.Text;
            objzjsj.StdValNO = no_bzz.Text;
            objzjsj.RateValNO = noxdc.Text;
            objzjsj.DivValNO = nojdc.Text;
            objzjsj.CheckJudgeNO = "1";

            objzjsj.CheckValNO2 = no2cyz.Text;
            objzjsj.StdValNO2 = no2bzz.Text;
            objzjsj.RateValNO2 = no2xdc.Text;
            objzjsj.DivValNO2 = no2jdc.Text;
            objzjsj.CheckJudgeNO2 = "1";

            objzjsj.CheckValCO2 = co2cyz.Text;
            objzjsj.StdValCO2 = co2bzz.Text;
            objzjsj.RateValCO2 = co2xdc.Text;
            objzjsj.DivValCO2 = co2jdc.Text;
            objzjsj.CheckJudgeCO2 = "1";

            objzjsj.dtCheckDate = DateTime.Now.ToString("yyyy-MM-dd");
            objzjsj.dtBeginTime = txtstartxl.Text;
            objzjsj.dtEndTime = txtendxl.Text;
            objzjsj.Checker = skinCom_czy.Text;

            MessageBox.Show(m_mainform.objbass.uploadzj(objzjsj));


            /*
            model.尾气自检.fqyddjc objxljc = new model.尾气自检.fqyddjc();
            objxljc.dtCheckDate = txtstartxl.Text;
            objxljc.dtBeginTime = txtstartxl.Text;
            objxljc.dtEndTime = txtendxl.Text;
            objxljc.CheckDate = txtstartxl.Text;
            objxljc.CheckType = "1";
            objxljc.PEF = "0";
            objxljc.CheckValHC = "0";
            objxljc.StdValHC = "0";
            objxljc.RateValHC = "0";
            objxljc.DivValHC = "0";
            objxljc.CheckJudgeHC = "1";
            objxljc.T10ValHC = "0";
            objxljc.T10StdHC = "0";
            objxljc.T10JudgeHC = "0";
            objxljc.T90ValHC = "0";
            objxljc.T90StdHC = "0";
            objxljc.T90JudgeHC = "0";
            objxljc.JudgeResultHC = "1";

            objxljc.CheckValCO = "0";
            objxljc.StdValCO = "0";
            objxljc.RateValCO = "0";
            objxljc.DivValCO = "0";
            objxljc.CheckJudgeCO = "1";
            objxljc.T10ValCO = "0";
            objxljc.T10StdCO = "0";
            objxljc.T10JudgeCO = "0";
            objxljc.T90ValCO = "0";
            objxljc.T90StdCO = "0";
            objxljc.T90JudgeCO = "0";
            objxljc.JudgeResultCO = "1";

            objxljc.CheckValNO = no_cyz.Text;
            objxljc.StdValNO = no_bzz.Text;
            objxljc.RateValNO = "0";
            objxljc.DivValNO = "0";
            objxljc.CheckJudgeNO = "1";
            objxljc.T10ValNO = "0";
            objxljc.T10StdNO = "0";
            objxljc.T10JudgeNO = "0";
            objxljc.T90ValNO = "0";
            objxljc.T90StdNO = "0";
            objxljc.T90JudgeNO = "0";
            objxljc.JudgeResultNO = "1";

            objxljc.CheckValCO2 = "0";
            objxljc.StdValCO2 = "0";
            objxljc.RateValCO2 = "0";
            objxljc.DivValCO2 = "0";
            objxljc.CheckJudgeCO2 = "1";
            objxljc.T10ValCO2 = "0";
            objxljc.T10StdCO2 = "0";
            objxljc.T10JudgeCO2 = "0";
            objxljc.T90ValCO2 = "0";
            objxljc.T90StdCO2 = "0";
            objxljc.T90JudgeCO2 = "0";
            objxljc.JudgeResultCO2 = "1";

            objxljc.CheckTypeex = "低浓度";
            objxljc.C3H8 = "0";
            objxljc.StdValO2 = "0";
            objxljc.CheckValO2 = "0";
            objxljc.Checker = skinCom_czy.Text;
            MessageBox.Show(m_mainform.objbass.uploadzj(objxljc));*/

        }

       

        private void no_cyz_TextChanged(object sender, EventArgs e)
        {
            if (!timer1.Enabled)
            {
                bnox = true;
                if (no_cyz.Text.Length > 0 && no2cyz.Text.Length > 0 && co2cyz.Text.Length > 0)
                    shuaxin();
            }
        }

        private void skinButton6_Click(object sender, EventArgs e)
        {
            txtstartxl.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            bnox = true;
            timer1.Enabled = true;
       
        }

        private void btgyiqijianding_Resize(object sender, EventArgs e)
        {
           
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            txtendxl.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //
            float fnoxjdc = 0, fnoxxdc = 0, fco2jdc = 0, fco2xdc = 0;
            float.TryParse(nojdc.Text, out fnoxjdc);
            float.TryParse(noxdc.Text, out fnoxxdc);
            float.TryParse(co2jdc.Text, out fco2jdc);
            float.TryParse(co2xdc.Text, out fco2xdc);
            if (fnoxjdc > 25 && fnoxxdc > 4)// || fco2jdc > 0 && fco2xdc > 0)
            {
                textBox5.Text = "不合格";
            }
            else
            {
                textBox5.Text = "合格";
            }
            timer1.Enabled = false;
            bnox = false;
            ////bldpy = false;
            m_mainform.objbtg.SendDeviceCmd(2);
            //skinButton1_Click(sender, e);
            //m_mainform.objbtg.SendDeviceCmd(24);
            string sjieguo = string.Format(@"NO标准值{0};NO检定值{1};NO2标准值{2};NO2检定值{3};CO2标准值{4};CO2检定值{5};",
                                       no_bzz.Text,
                                        no_cyz.Text,
                                        no2bzz.Text,
                                        no2cyz.Text,
                                        co2bzz.Text,
                                        co2cyz.Text);
            string ssqlex = string.Format(@"insert into JDSJ (
                                               JDSJ_MC,
                                               JDSJ_JG,
                                               JDSJ_RQ,
                                               JDSJ_JCX) values (
                                                '{0}',
                                                '{1}',
                                                '{2}',
                                                '{3}')",
                                         "氮氧化合物仪",
                                         sjieguo,
                                         DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                         m_mainform.lineCode);
            m_mainform.objbassLW.objdb.InsertDB(ssqlex);

            iniclass objini = new iniclass();
            objini.Ini(System.Windows.Forms.Application.StartupPath + "\\设备检定结果.ini");

            objini.Writue("柴油分析仪检定", "标准NO",no_bzz.Text);
            objini.Writue("柴油分析仪检定", "检定NO", no_cyz.Text);
            objini.Writue("柴油分析仪检定", "标准CO2", co2bzz.Text);
            objini.Writue("柴油分析仪检定", "检定CO2", co2cyz.Text);
            //objini.Writue("柴油分析仪检定", "判定", text_dpjg.Text);
            objini.Writue("柴油分析仪检定", "检定时间", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

        }

        private void T10CO_TextChanged(object sender, EventArgs e)
        {
            float t90nox = 0, t10nox = 0;
            if (T90NOx.TextLength > 0)
            {
                float.TryParse(T90NOx.Text, out t90nox);
                if (t90nox < 6.5)
                {
                    T90NOxPD.Text = "合格";
                }
                else
                {
                    T90NOxPD.Text = "不合格";
                }
            }
            if (T10NOx.TextLength > 0)
            {
                float.TryParse(T10NOx.Text, out t10nox);
                if (t10nox < 6.7)
                {
                    T10NOxPD.Text = "合格";
                }
                else
                {
                    T10NOxPD.Text = "不合格";
                }
            }
        }

        private void skinButton11_Click(object sender, EventArgs e)
        {
            bSK = true;
            bnox = false;
            bssxysj = true;
            bxxxysj = false;
            tqsj = DateTime.Now;
            timer1.Enabled = true;
        }
        public static double DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            double dateDiff = 0;
            TimeSpan ts = DateTime1.Subtract(DateTime2).Duration();
            dateDiff = ts.Days * 60 * 60 * 60 * 1000 + ts.Hours * 60 * 60 * 1000 + ts.Minutes * 60 * 1000 + ts.Seconds * 1000 + ts.Milliseconds;
            return dateDiff;
        }

        private void skinButton10_Click(object sender, EventArgs e)
        {
            tztqsj = DateTime.Now;
        }

        private void skinButton9_Click(object sender, EventArgs e)
        {         
            m_mainform.objbtg.SendDeviceCmd(3);
            timer1.Enabled = false;
        }

        DateTime dtimet90 = DateTime.Now;
        DateTime dtimet10 = DateTime.Now;
        bool bt90start = false;
        bool bt10start = false;
        bool bxysjno = true;
        private void skinButton13_Click(object sender, EventArgs e)
        {
            m_mainform.objbtg.SendDeviceCmd(10);
            dtimet90 = DateTime.Now;
            bt90start = true;
            timer1.Enabled = true;
            bxysjno = true;
        }

        private void skinButton14_Click(object sender, EventArgs e)
        {
            bt90start = false;
            m_mainform.objbtg.SendDeviceCmd(2);
            bxysjno = false;
          
        }

        private void skinButton16_Click(object sender, EventArgs e)
        {
            m_mainform.objbtg.SendDeviceCmd(1);
            dtimet10 = DateTime.Now;
            bt10start = true;
            timer1.Enabled = true;
            bxysjno = true;    
        }

        private void skinButton15_Click(object sender, EventArgs e)
        {
            m_mainform.objbtg.SendDeviceCmd(2);
            bt10start = false;
            bxysjno = false;
        }
        public void getldpy()
        {
            text_dpjg.Text = "测量中";
            if (text_kzhi.Text.Length < 1)
                text_kzhi.Text = "0";
            float fkzhi = 0;
            float.TryParse(text_kzhi.Text, out fkzhi);
            //if (k0cyz.Text.Length < 1)
            //    k0cyz.Text = "0";
            //k0cyz.Text = string.Format(@"{0}", m_mainform.objbtg.m_fnssk > float.Parse(k0cyz.Text) ? m_mainform.objbtg.m_fnssk : float.Parse(k0cyz.Text));
            text_kzhi.Text = string.Format(@"{0}", m_mainform.objbtg.m_fnssk > fkzhi ? m_mainform.objbtg.m_fnssk : fkzhi);
            if (m_mainform.DateDiff(DateTime.Now, dtimetldpy) > 1000 * 600)
            {
                text_dpjg.Text = "合格";
                if (Math.Abs(float.Parse(text_kzhi.Text)) > 1)
                {
                    text_dpjg.Text = "不合格";
                }
                bldpy = false;
                timer1.Enabled = false;
            }
        }
        public void gett90()
        {
            float ft90xzno = 0;
            float.TryParse(T90NOXBZZ.Text, out ft90xzno);

            float fxzno = 0;
            float.TryParse(no_bzz.Text, out fxzno);
         

            double dxysj = Math.Round(DateDiff(DateTime.Now, dtimet90) * 0.001, 1);
            if (bxysjno && fxzno * 0.9f <= m_mainform.objbtg.m_fnosk) { bxysjno = false; T90NOx.Text = dxysj.ToString("0.0"); T90NOxPD.Text = dxysj > ft90xzno ? "不合格" : "合格"; bt90start = false; }
          
        }

        public void gett10()
        {
            float ft10xzno = 0;
            float.TryParse(T10NOXBZZ.Text, out ft10xzno);          

            float fxzno = 0;
            float.TryParse(no_bzz.Text, out fxzno);

            double dxysj = Math.Round(DateDiff(DateTime.Now, dtimet10) * 0.001, 1);
            if (bxysjno && fxzno * 0.1f >= m_mainform.objbtg.m_fnosk) { bxysjno = false; T10NOx.Text = dxysj.ToString("0.0"); T10NOxPD.Text = dxysj > ft10xzno ? "不合格" : "合格"; bt10start = false; }
       
        }
        public void shuaxin(int ikindex = 1)
        {
            if (bnox)
            {
                double xdz = 0;
                float f1 = 0, f2 = 0, f3 = 0;
                float.TryParse(no_cyz.Text, out f1);
                float.TryParse(no_bzz.Text, out f2);
                nojdc.Text = Math.Abs(f1 - f2).ToString("0.00");
                xdz = (Math.Abs(f1 - f2) / f2 * 100f);
                noxdc.Text = xdz.ToString("0.00");


                float.TryParse(no2cyz.Text, out f1);
                float.TryParse(no2bzz.Text, out f2);
                no2jdc.Text = Math.Abs(f1 - f2).ToString("0.00");
                if (Math.Abs(f1 - f2) == 0)
                {
                    no2xdc.Text = "-";
                }
                else
                {
                    no2xdc.Text = (Math.Abs(f1 - f2) / f2 * 100f).ToString("0.00");
                }

                float.TryParse(co2cyz.Text, out f1);
                float.TryParse(co2bzz.Text, out f2);
                co2jdc.Text = Math.Abs(f1 - f2).ToString("0.00");
                co2xdc.Text = (Math.Abs(f1 - f2) / f2 * 100f).ToString("0.00");

            }
            else//ns
            {
                float f1 = 0, f2 = 0;
                float.TryParse(k0cyz.Text, out f1);
                float.TryParse(k0bzz.Text, out f2);
                //if ((bns0) || (bldpy))
                if (k0cyz.Text.Trim().Length > 0)
                    k0jdc.Text = Math.Abs(f1 - f2).ToString("0.00");

                float.TryParse(k30cyz.Text, out f1);
                float.TryParse(k30bzz.Text, out f2);
                //if (bns30)
                if (k30cyz.Text.Trim().Length > 0)
                    k30jdc.Text = Math.Abs(f1 - f2).ToString("0.00");

                float.TryParse(k50cyz.Text, out f1);
                float.TryParse(k50bzz.Text, out f2);
                //if (bns50)
                if (k50cyz.Text.Trim().Length > 0)
                    k50jdc.Text = Math.Abs(f1 - f2).ToString("0.00");

                float.TryParse(k70cyz.Text, out f1);
                float.TryParse(k70bzz.Text, out f2);
                //if (bns70)
                if (k70cyz.Text.Trim().Length > 0)
                    k70jdc.Text = Math.Abs(f1 - f2).ToString("0.00");

                float.TryParse(k100cyz.Text, out f1);
                float.TryParse(k100bzz.Text, out f2);
                //if (bns100)
                if (k100cyz.Text.Trim().Length > 0)
                    k100jdc.Text = Math.Abs(f1 - f2).ToString("0.00");
                bool bbtgpd = true;
                //if (!bldpy && bns0) 
                //if (ikindex == 0)
                if (k0cyz.Text.Length > 0 && float.Parse(k0jdc.Text) > 2)
                {
                    bbtgpd = false;
                    //text_dpjg.Text = float.Parse(k0jdc.Text) > 2 ? "不合格" : "合格";
                }
                //if (!bldpy && bns30) 
                //if (ikindex == 30)
                if (k30cyz.Text.Length > 0 && float.Parse(k30jdc.Text) > 2)
                {
                    bbtgpd = false;
                    //text_dpjg.Text = float.Parse(k50jdc.Text) > 2 ? "不合格" : "合格";
                }
                //if (!bldpy && bns50) 
                //if (ikindex == 50)
                if (k50cyz.Text.Length > 0 && float.Parse(k50jdc.Text) > 2)
                {
                    bbtgpd = false;
                    //text_dpjg.Text = float.Parse(k50jdc.Text) > 2 ? "不合格" : "合格";
                }
                //if (!bldpy && bns70) 
                //if (ikindex == 70)
                if (k70cyz.Text.Length > 0 && float.Parse(k70jdc.Text) > 2)
                {
                    bbtgpd = false;
                    //text_dpjg.Text = float.Parse(k70jdc.Text) > 2 ? "不合格" : "合格";
                }
                //if (!bldpy && bns100) 
                //if (ikindex == 100)
                if (k100cyz.Text.Length > 0 && float.Parse(k100jdc.Text) > 2)
                {
                    bbtgpd = false;
                    //text_dpjg.Text = float.Parse(k100jdc.Text) > 2 ? "不合格" : "合格";
                }
                text_dpjg.Text = bbtgpd ? "合格" : "不合格";
            }
        }

        bool bns0 = false;
        bool bns30 = false;
        bool bns50 = false;
        bool bns70 = false;
        bool bns100 = false;
        private void ks0_Click(object sender, EventArgs e)
        {
            bns0 = true;
            bldpy = false;
            k0cyz.Text = text_kzhi.Text;
            //if (!timer1.Enabled)
            {
                shuaxin(0);
            }
        }

        private void ks30_Click(object sender, EventArgs e)
        {
            bns30 = true;
            bldpy = false;
            k30cyz.Text = text_kzhi.Text;
            //if (!timer1.Enabled)
            {
                shuaxin(30);
            }


        }

        private void ks50_Click(object sender, EventArgs e)
        {
            bns50 = true;
            bldpy = false;
            k50cyz.Text = text_kzhi.Text;
            //if (!timer1.Enabled)
            {
                shuaxin(50);
            }
        }

        private void ks70_Click(object sender, EventArgs e)
        {
            bns70 = true;
            bldpy = false;
            k70cyz.Text = text_kzhi.Text;
            //if (!timer1.Enabled)
            {
                shuaxin(70);
            }
        }

        private void ks100_Click(object sender, EventArgs e)
        {
            bns100 = true;
            bldpy = false;
            k100cyz.Text = text_kzhi.Text;
            //if (!timer1.Enabled)
            {
                shuaxin(100);
            }
        }

        DateTime dtimetldpy = DateTime.Now;
        private void skinButton10_Click_1(object sender, EventArgs e)
        {
            txtstartxl.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //k0cyz.Text = "0";
            bldpy = true;
            bnox = false;
            dtimetldpy = DateTime.Now;
            m_mainform.objbtg.SendDeviceCmd(1);//
            timer1.Enabled = true;
        }

        private void skinButton9_Click_1(object sender, EventArgs e)
        {
            txtendxl.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            bldpy = false;
            m_mainform.objbtg.SendDeviceCmd(2);
            timer1.Enabled = false;
            Thread.Sleep(100);
            text_dpjg.Text = "零点偏移结束";
            //text_dpjg.Text = "合格";
            //if (Math.Abs(float.Parse(text_kzhi.Text)) > 1)
            //{
            //    text_dpjg.Text = "不合格";
            //}
        }

        private void skinButton11_Click_1(object sender, EventArgs e)
        {
            bns0 = false;
            bns30 = false;
            bns50 = false;
            bns70 = false;
            bns100 = false;
            bnox = false;
            bldpy = false;
            timer1.Enabled = false;
            m_mainform.objbtg.SendDeviceCmd(2);
        }
        private void skinButton17_Click(object sender, EventArgs e)
        {
            iniclass objini = new iniclass();
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + "\\检测自检\\加载减速设备自检信息.ini";
            string path1 = string.Format(@"{0}", path);
            objini.Ini(path1);
            if (CBBND.Text.Contains("低浓度"))
            {
                objini.Writue("记录", "Low_NO2", "0");
                objini.Writue("记录", "Low_NO2_Error", "0");
                objini.Writue("记录", "Low_NO2_Judge", "0");

                objini.Writue("记录", "Low_CO2", tb_cosc2.Text);
                objini.Writue("记录", "Low_CO2_Error", tb_cowc2.Text);
                objini.Writue("记录", "Low_CO2_Judge", "1");

                objini.Writue("记录", "Low_NO", tb_nosc.Text);
                objini.Writue("记录", "Low_NO_Error", tb_nowc.Text);
                objini.Writue("记录", "Low_NO_Judge", "1");
            }
            else if (CBBND.Text.Contains("中低浓度"))
            {
                objini.Writue("记录", "ML_NO2", "0");
                objini.Writue("记录", "ML_NO2_Error", "0");
                objini.Writue("记录", "ML_NO2_Judge", "0");

                objini.Writue("记录", "ML_CO2", tb_cosc2.Text);
                objini.Writue("记录", "ML_CO2_Error", tb_cowc2.Text);
                objini.Writue("记录", "ML_CO2_Judge", "1");

                objini.Writue("记录", "ML_NO", tb_nosc.Text);
                objini.Writue("记录", "ML_NO_Error", tb_nowc.Text);
                objini.Writue("记录", "ML_NO_Judge", "1");
            }
            else if (CBBND.Text.Contains("中高浓度"))
            {
                objini.Writue("记录", "MH_NO2", "0");
                objini.Writue("记录", "MH_NO2_Error", "0");
                objini.Writue("记录", "MH_NO2_Judge", "0");
                objini.Writue("记录", "MH_CO2", tb_cosc2.Text);
                objini.Writue("记录", "MH_CO2_Error", tb_cowc2.Text);
                objini.Writue("记录", "MH_CO2_Judge", "1");
                objini.Writue("记录", "MH_NO", tb_nosc.Text);
                objini.Writue("记录", "MH_NO_Error", tb_nowc.Text);
                objini.Writue("记录", "MH_NO_Judge", "1");
            }
            else if (CBBND.Text.Contains("高浓度"))
            {
                objini.Writue("记录", "High_NO2", "0");
                objini.Writue("记录", "High_NO2_Error", "0");
                objini.Writue("记录", "High_NO2_Judge", "0");
                objini.Writue("记录", "High_CO2", tb_cosc2.Text);
                objini.Writue("记录", "High_CO2_Error", tb_cowc2.Text);
                objini.Writue("记录", "High_CO2_Judge", "1");
                objini.Writue("记录", "High_NO", tb_nosc.Text);
                objini.Writue("记录", "High_NO_Error", tb_nowc.Text);
                objini.Writue("记录", "High_NO_Judge", "1");
            }
            else if (CBBND.Text.Contains("零"))
            {
                objini.Writue("记录", "Zero_NO2", "0");
                objini.Writue("记录", "Zero_NO2_Error", "0");
                objini.Writue("记录", "Zero_NO2_Judge", "0");
                objini.Writue("记录", "Zero_CO2", tb_cosc2.Text);
                objini.Writue("记录", "Zero_CO2_Error", tb_cowc2.Text);
                objini.Writue("记录", "Zero_CO2_Judge", "1");
                objini.Writue("记录", "Zero_NO", tb_nosc.Text);
                objini.Writue("记录", "Zero_NO_Error", tb_nowc.Text);
                objini.Writue("记录", "Zero_NO_Judge", "1");
            }
            else
            {
                objini.Writue("记录", "Medium_NO2", "0");
                objini.Writue("记录", "Medium_NO2_Error", "0");
                objini.Writue("记录", "Medium_NO2_Judge", "0");
                objini.Writue("记录", "Medium_CO2", tb_cosc2.Text);
                objini.Writue("记录", "Medium_CO2_Error", tb_cowc2.Text);
                objini.Writue("记录", "Medium_CO2_Judge", "1");
                objini.Writue("记录", "Medium_NO", tb_nosc.Text);
                objini.Writue("记录", "Medium_NO_Error", tb_nowc.Text);
                objini.Writue("记录", "Medium_NO_Judge", "1");
            }
        }

        private void skinButton12_Click(object sender, EventArgs e)
        {
            联网通信协议.云景model.HJ25 objcybd = new 联网通信协议.云景model.HJ25();
            objcybd.StationCode = m_mainform.StationCode;
            objcybd.SceneCode = m_mainform.lineCode;
            objcybd.EL = string.Format("{0}", 35.7 + m_mainform.getRandom(5, 9) * 0.1);
            objcybd.ER = string.Format("{0}", 35.7 + m_mainform.getRandom(1, 5) * 0.1);
            objcybd.ED = "1";
            objcybd.NOX = "50";
            objcybd.NOXEL = string.Format("{0}", 50 + m_mainform.getRandom(0, 5));
            objcybd.NOXED = "1";
            objcybd.AdjustTimeStart = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            objcybd.AdjustTimeEnd = DateTime.Now.AddHours(1).ToString("yyyy-MM-dd HH:mm:ss");

            string sjieguo = string.Format(@"柴油线标定信息：{0}", JsonConvert.SerializeObject(objcybd));
            string ssqlex = string.Format(@"insert into JDSJ (
                                               JDSJ_MC,
                                               JDSJ_JG,
                                               JDSJ_RQ,
                                               JDSJ_JCX) values (
                                                '{0}',
                                                '{1}',
                                                '{2}',
                                                '{3}')",
                                         "柴油线标定信息",
                                         sjieguo,
                                         DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                         m_mainform.lineCode);
            m_mainform.objbassLW.objdb.InsertDB(ssqlex);
            MessageBox.Show(m_mainform.objbass.uploadzj(objcybd));
        }

        public void getqiti(string ranyou, string qiti)
        {
            string sql = string.Format("select * from qititable where ranyou='{0}' and qitileixing='{1}'", ranyou, qiti);
            DataTable dt = m_mainform.objdbbass.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                no_bzz.Text = dt.Rows[0]["NO"].ToString().Trim().Length > 0 ? dt.Rows[0]["NO"].ToString().Trim() : "0";    //填充第二列的数据 
                co2bzz.Text = dt.Rows[0]["CO2"].ToString().Trim().Length > 0 ? dt.Rows[0]["CO2"].ToString().Trim() : "0";    //填充第二列的数据 
            }
        }

        private void CBBND_SelectedIndexChanged(object sender, EventArgs e)
        {
            getqiti("柴油", CBBND.Text);
        }
        public void getN()
        {
            string sql = string.Format("select * from btgtable");
            DataTable dt = m_mainform.objdbbass.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                k30bzz.Text = dt.Rows[0]["N30"].ToString().Trim();
                k50bzz.Text = dt.Rows[0]["N50"].ToString().Trim();
                k70bzz.Text = dt.Rows[0]["N70"].ToString().Trim();
            }
        }

        private void btgyiqijianding_Shown(object sender, EventArgs e)
        {
            float newx = (this.Width) / x;
            float newy = (this.Height) / y;
            setControls(newx, newy, this);
        }
    }
}
